﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace TestPing
{
    public class PingResult : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        //推流
        private List<long> _PushTimes = new List<long>();
        private long _PushMinTime;
        private long _PushMaxTime;
        private long _PushAveTime;
        private long _PushCount;//成功次数
        private string _PushResult;//结果
        private long _PushFailure;//失败次数
        private string _PushPercentage;//失败百分比



        //DNS
        private List<long> _DnsTimes = new List<long>();
        private long _DnsMinTime;
        private long _DnsMaxTime;
        private long _DnsAveTime;
        private long _DnsCount;
        private string _DnsResult;
        private long _DnsFailure;//失败次数
        private string _DnsPercentage;//失败百分比

        public long PushMinTime
        {
            get
            {
                return this._PushMinTime;
            }
            set
            {
                if (this._PushMinTime != value) //如果当前的变量值不等于先前的值，说明需要更新文件名  
                {
                    this._PushMinTime = value;  //更新值                                                                                     
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushMinTime")); //通知绑定此变量的textbox在前台更新  
                }
            }
        }

        public long PushMaxTime
        {
            get
            {
                return this._PushMaxTime;
            }
            set
            {
                if (this._PushMaxTime != value)
                {
                    this._PushMaxTime = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushMaxTime"));
                }
            }
        }

        public long PushAveTime
        {
            get
            {
                return this._PushAveTime;
            }
            set
            {
                if (this._PushAveTime != value)
                {
                    this._PushAveTime = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushAveTime"));
                }
            }
        }

        public long PushCount
        {
            get
            {
                return this._PushCount;
            }
            set
            {
                if (this._PushCount != value)
                {
                    this._PushCount = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushCount"));
                }
            }
        }

        public string PushResult
        {
            get
            {
                return this._PushResult;
            }
            set
            {
                if (this._PushResult != value)
                {
                    this._PushResult = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushResult"));
                }
            }
        }

        public long PushFailure
        {
            get
            {
                return this._PushFailure;
            }
            set
            {
                if (this._PushFailure != value)
                {
                    this._PushFailure = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushFailure"));
                }
            }
        }

        public string PushPercentage
        {
            get
            {
                return this._PushPercentage;
            }
            set
            {
                if (this._PushPercentage != value)
                {
                    this._PushPercentage = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PushPercentage"));
                }
            }
        }


        public void AddPushResult(PingReply reply)
        {
            if (reply.Status == IPStatus.Success)
            {
                //    Console.WriteLine("答复的主机地址：" + reply.Address.ToString());
                //    Console.WriteLine("往返时间：" + reply.RoundtripTime);
                //    Console.WriteLine("生存时间（TTL）：" + reply.Options.Ttl);
                //    Console.WriteLine("是否控制数据包的分段：" + reply.Options.DontFragment);
                //    Console.WriteLine("缓冲区大小：" + reply.Buffer.Length);
                this._PushTimes.Add(reply.RoundtripTime);
                this.PushMinTime = _PushTimes.Min();
                this.PushMaxTime = _PushTimes.Max();
                this.PushAveTime = _PushTimes.Sum() / _PushTimes.Count();
                this.PushCount = _PushTimes.Count();
                this.PushResult = "推流结果=主机:" + reply.Address.ToString() + " 时间:" + reply.RoundtripTime + " TTL:" + reply.Options.Ttl;
            }
            else
            {
                this.PushResult = reply.Status.ToString();
                this.PushFailure++;               
            }
            this.PushPercentage = Math.Round(((Double)this.PushFailure / (Double)(this._PushTimes.Count() + this.PushFailure)), 2) + "%";
        }


        public long DnsMinTime
        {
            get
            {
                return this._DnsMinTime;
            }
            set
            {
                if (this._DnsMinTime != value)
                {
                    this._DnsMinTime = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsMinTime"));
                }
            }
        }

        public long DnsMaxTime
        {
            get
            {
                return this._DnsMaxTime;
            }
            set
            {
                if (this._DnsMaxTime != value)
                {
                    this._DnsMaxTime = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsMaxTime"));
                }
            }
        }

        public long DnsAveTime
        {
            get
            {
                return this._DnsAveTime;
            }
            set
            {
                if (this._DnsAveTime != value)
                {
                    this._DnsAveTime = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsAveTime"));
                }
            }
        }

        public long DnsCount
        {
            get
            {
                return this._DnsCount;
            }
            set
            {
                if (this._DnsCount != value)
                {
                    this._DnsCount = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsCount"));
                }
            }
        }

        public string DnsResult
        {
            get
            {
                return this._DnsResult;
            }
            set
            {
                if (this._DnsResult != value)
                {
                    this._DnsResult = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsResult"));
                }
            }
        }

        public long DnsFailure
        {
            get
            {
                return this._DnsFailure;
            }
            set
            {
                if (this._DnsFailure != value)
                {
                    this._DnsFailure = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsFailure"));
                }
            }
        }

        public string DnsPercentage
        {
            get
            {
                return this._DnsPercentage;
            }
            set
            {
                if (this._DnsPercentage != value)
                {
                    this._DnsPercentage = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DnsPercentage"));
                }
            }
        }

        public void AddDnsResult(PingReply reply)
        {
            if (reply.Status == IPStatus.Success)
            {
                this._DnsTimes.Add(reply.RoundtripTime);
                this.DnsMinTime = _DnsTimes.Min();
                this.DnsMaxTime = _DnsTimes.Max();
                this.DnsAveTime = _DnsTimes.Sum() / _DnsTimes.Count();
                this.DnsCount = _DnsTimes.Count();
                this.DnsResult = "DNS=主机:" + reply.Address.ToString() + " 时间:" + reply.RoundtripTime + " TTL:" + reply.Options.Ttl;
            }
            else
            {
                this.DnsResult = reply.Status.ToString();
                this.DnsFailure++;              
            }
            this.DnsPercentage = Math.Round(((Double)this.DnsFailure / (Double)(this._DnsTimes.Count() + this.DnsFailure)), 2) + "%";
        }

    }

}
